package cn.kgc.controller;



import cn.kgc.pojo.SmbmsProvider;
import cn.kgc.service.ProService;
import cn.kgc.utils.PageInfo;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("pro")
public class ProController {
    private Logger logger = Logger.getLogger(ProController.class);
    @Resource
    ProService proService;

    @RequestMapping("/limitPro")
    public String limit(@RequestParam(value = "currentPageNo",required = false) Integer currentPageNo,
                        @RequestParam(value = "pageSize",required = false) Integer pageSize,
                        @RequestParam(value = "proName",required = false) String proName,
                        Model model){
        PageInfo pageInfo2 = new PageInfo();
        if (currentPageNo != null) {
            pageInfo2.setCurrentPageNo(currentPageNo);
        }
        if (pageSize != null){
            pageInfo2.setPageSize(pageSize);
        }
        int totalConut = proService.getProConut(proName);
        pageInfo2.setTotalConut(totalConut);
        List<SmbmsProvider> listp = proService.getProByPage(pageInfo2.getCurrentPageNo(),pageInfo2.getPageSize(),proName);
        pageInfo2.setData(listp);
        model.addAttribute("page",pageInfo2);
        model.addAttribute("proName",proName);
        return "indexPro";
    }

    @RequestMapping("/toAddPro")
    public String toAddPro(){
        return "proAdd";
    }
    @RequestMapping("/addPro")
    public String addPro(SmbmsProvider smbmsProvider){
        boolean flag = proService.addPro(smbmsProvider);
        if (flag){
            return "redirect:/pro/limitPro";
        }else {
            return "proAdd";
        }
    }

    @RequestMapping("/toDelPro")
    public String toDelPro(@RequestParam(value = "id",required = false) Integer id){
        boolean flag = proService.delProId(id);
        if (flag){
            return "redirect:/pro/limitPro";
        }else {
            return "indexPro";
        }

    }

    @RequestMapping("/toProUpdate")
    public String toProUpdate(@RequestParam("id")Integer id,Model model){
        SmbmsProvider smbmsProvider = proService.getProId(id);
        model.addAttribute("pro",smbmsProvider);
        return "proUpdate";
    }
    @RequestMapping(value = "/updatePro",method = RequestMethod.POST)
    public String updatePro(SmbmsProvider smbmsProvider){
        boolean flag = proService.updatePro(smbmsProvider);
        if (flag){
            return "redirect:/pro/limitPro";
        }else {
            return "proUpdate";
        }
    }

    @RequestMapping("/toProView")
    public String toProView(@RequestParam("id")Integer id,Model model){
        SmbmsProvider smbmsProvider = proService.getProId(id);
        model.addAttribute("pro",smbmsProvider);
        return "proView";
    }

    @RequestMapping("/proIsExist/{proCode}")
    @ResponseBody
    public JSONObject proIsExist(@PathVariable String proCode){

        logger.info("billCode======="+proCode);
        SmbmsProvider smbmsBill = proService.getProCode(proCode);
        JSONObject jsonObject = new JSONObject();
        if (smbmsBill == null){
            jsonObject.put("code","10000");
            jsonObject.put("msg","可以使用");
        }else {
            jsonObject.put("code","10001");
            jsonObject.put("msg","不可以使用");
        }
        return jsonObject;
    }


    @RequestMapping("/getProId/{id}")
    @ResponseBody
    public SmbmsProvider getProId(@PathVariable Integer id){
        SmbmsProvider smbmsProvider = proService.getProId(id);
        return smbmsProvider;
    }
}
